package com.qing.demo01.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		String uuid = request.getHeader("uuid");
		if (request.getRequestURI().contains("login")) {
			return true;
		}


		if (!StringUtils.hasLength(uuid)) {
			//δ��¼
			response.sendRedirect("/login/page");
			return false;
		} else if (request.getSession().getAttribute("uuid").equals(uuid)) {
			return true;
		} else {
			//�˺��������
			response.sendRedirect("/login/page");
			return false;
		}
	}


	//������־
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.out.println("=========����=====");
	}

	//������־
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("=========����=====");
	}
}
